/**
 * 发表
 */
function publish() {
    let title = $('#title').val().trim();
    let introduce = $('#info').val().trim();
    let courseUrl = $('#courseUrl').val().trim();
    let courseSections = $('#courseSections').val().trim();
    let courseTime = $('#courseTime').val().trim();
    let courseTypeId = $('#type').find('option:selected').val();
    let courseStageId = $('#stage').find('option:selected').val();
    let sort = $('#sort').val().trim();
    console.log(sort);

    if (title == ''){
        swal("警告","标题不能为空！", "warning");
        return;
    }else if(introduce == ''){
        swal("警告","课程简介不能为空！", "warning");
        return;
    }else if(courseUrl == ''){
        swal("警告","课程链接不能为空！", "warning");
        return;
    }else if(courseSections == ''){
        swal("警告","请输入课程节数！", "warning");
        return;
    }else if(courseTime == ''){
        swal("警告","课程时长不能为空！", "warning");
        return;
    }else if(courseTypeId == '0'){
        swal("警告","请选择课程类别！", "warning");
        return;
    }else if(courseStageId == '0'){
        swal("警告","请选择课程阶段！", "warning");
        return;
    }else if(sort == ''){
        swal("警告","请输入课程排序！", "warning");
        return;
    }
    $.ajax({
        type: "POST",
        url: "publish",
        data: {title:title,introduce:introduce,courseUrl:courseUrl,courseSections:courseSections,courseTime:courseTime,courseTypeId:courseTypeId,courseStageId:courseStageId,sort:sort},
        dataType: "json",
        success: function(data){
            if (data['code']== 200){
                swal("发表成功", "你现在可以给课程添加一个封面啦！", "success")
                    .then(() => {
                        var location = window.location.href;
                        let strings = location.split("iStudy");
                        window.location.href = strings[0]+"iStudy/edit?id="+data['data'];
                    });
            }else{
                swal("出错啦", "服务器发生了一个错误", "error");
            }
        }
    });
}

/**
 * 草稿发布
 */
function draftPublish() {
    let title = $('#title').val().trim();
    let introduce = $('#info').val().trim();
    let courseUrl = $('#courseUrl').val().trim();
    let courseSections = $('#courseSections').val().trim();
    let courseTime = $('#courseTime').val().trim();
    let courseTypeId = $('#type').find('option:selected').val();
    let courseStageId = $('#stage').find('option:selected').val();
    let sort = $('#sort').val().trim();
    if (title == ''){
        swal("警告","标题不能为空！", "warning");
        return;
    }else if(introduce == ''){
        swal("警告","课程简介不能为空！", "warning");
        return;
    }else if(courseUrl == ''){
        swal("警告","课程链接不能为空！", "warning");
        return;
    }else if(courseSections == ''){
        swal("警告","请输入课程节数！", "warning");
        return;
    }else if(courseTime == ''){
        swal("警告","课程时长不能为空！", "warning");
        return;
    }else if(courseTypeId == '0'){
        swal("警告","请选择课程类别！", "warning");
        return;
    }else if(courseStageId == '0'){
        swal("警告","请选择课程阶段！", "warning");
        return;
    }else if(sort == ''){
        swal("警告","请输入课程排序！", "warning");
        return;
    }
    $.ajax({
        type: "POST",
        url: "draftPublish",
        data: {title:title,introduce:introduce,courseUrl:courseUrl,courseSections:courseSections,courseTime:courseTime,courseTypeId:courseTypeId,courseStageId:courseStageId,sort:sort},
        dataType: "json",
        success: function(data){
            if (data['code']== 200){
                swal("保存成功", "你可以在草稿箱找到它！", "success")
                    .then(() => {
                        var location = window.location.href;
                        let strings = location.split("iStudy");
                        window.location.href = strings[0]+"iStudy/edit?id="+data['data'];
                    });
            }else{
                swal("出错啦", "服务器发生了一个错误", "error");
            }
        }
    });
}

/**
 * 移入回收站
 */
function deleteLight(id,from) {
    swal({
        title: "确定将此课程移入回收站?",
        text: "你可以在回收站找到它，并且可以重新发布！",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    })
        .then((willDelete) => {
            if (willDelete) {
                var location = window.location.href;
                let strings = location.split("iStudy");
                window.location.href = strings[0]+"iStudy/deleteLight?id="+id+"&from="+from;
            }
        });
}

/**
 * 永久删除
 */
function deleteStrong(id,from) {
    swal({
        title: "确定将此课程永久删除?",
        text: "注意此操作不可恢复！",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    })
        .then((willDelete) => {
            if (willDelete) {
                var location = window.location.href;
                let strings = location.split("iStudy");
                window.location.href = strings[0]+"iStudy/deleteStrong?id="+id+"&from="+from;
            }
        });
}

/*
* 清空回收站
* */
function cleanTrash(){
    swal({
        title: "确定清空回收站？",
        text: "注意此操作不可恢复!",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    })
        .then((willDelete)=>{
           if(willDelete){
               let location = window.location.href;
               let strings = location.split("iStudy");
               window.location.href = strings[0]+"iStudy/cleanTrash";
           }
        });
}

/**
 *还原文章（放入草稿箱）
 * */
function restore(id,from){
    swal({
        title: "确定还原课程？",
        text: "此操作将把课程放入草稿箱",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    })
        .then((willDelete)=>{
            if(willDelete){
                let location = window.location.href;
                let strings = location.split("iStudy");
                window.location.href = strings[0]+"iStudy/restore?id="+id+"&from="+from;
            }
        })
}

/**
 * 更新
 */
function updatePublish() {
    let id = $('#id').val().trim();
    let title = $('#title').val().trim();
    let introduce = $('#info').val().trim();
    let courseUrl = $('#courseUrl').val().trim();
    let courseSections = $('#courseSections').val().trim();
    let courseTime = $('#courseTime').val().trim();
    let courseTypeId = $('#type').find('option:selected').val();
    let courseStageId = $('#stage').find('option:selected').val();
    let sort = $('#sort').val().trim();
    if (title == ''){
        swal("警告","标题不能为空！", "warning");
        return;
    }else if(introduce == ''){
        swal("警告","课程简介不能为空！", "warning");
        return;
    }else if(courseUrl == ''){
        swal("警告","课程链接不能为空！", "warning");
        return;
    }else if(courseSections == ''){
        swal("警告","请输入课程节数！", "warning");
        return;
    }else if(courseTime == ''){
        swal("警告","课程时长不能为空！", "warning");
        return;
    }else if(courseTypeId == '0'){
        swal("警告","请选择课程类别！", "warning");
        return;
    }else if(courseStageId == '0'){
        swal("警告","请选择课程阶段！", "warning");
        return;
    }else if(sort == ''){
        swal("警告","请输入课程排序！", "warning");
        return;
    }
    $.ajax({
        type: "POST",
        url: "update",
        data: {id:id,title:title,introduce:introduce,courseUrl:courseUrl,courseSections:courseSections,courseTime:courseTime,courseTypeId:courseTypeId,courseStageId:courseStageId,status:1,sort:sort},
        dataType: "json",
        success: function(data){
            if (data['code']== 200){
                swal("更新成功", "", "success")
                    .then(() => {
                        var location = window.location.href;
                        let strings = location.split("iStudy");
                        window.location.href = strings[0]+"iStudy/edit?id="+data['data'];
                    });
            }else{
            }
        }
    });
}

/**
 * 更新文章到草稿
 */
function updateDraft() {
    let id = $('#id').val().trim();
    let title = $('#title').val().trim();
    let introduce = $('#info').val().trim();
    let courseUrl = $('#courseUrl').val().trim();
    let courseSections = $('#courseSections').val().trim();
    let courseTime = $('#courseTime').val().trim();
    let courseTypeId = $('#type').find('option:selected').val();
    let courseStageId = $('#stage').find('option:selected').val();
    let sort = $('#sort').val().trim();
    if (title == ''){
        swal("警告","标题不能为空！", "warning");
        return;
    }else if(introduce == ''){
        swal("警告","课程简介不能为空！", "warning");
        return;
    }else if(courseUrl == ''){
        swal("警告","课程链接不能为空！", "warning");
        return;
    }else if(courseSections == ''){
        swal("警告","请输入课程节数！", "warning");
        return;
    }else if(courseTime == ''){
        swal("警告","课程时长不能为空！", "warning");
        return;
    }else if(courseTypeId == '0'){
        swal("警告","请选择课程类别！", "warning");
        return;
    }else if(courseStageId == '0'){
        swal("警告","请选择课程阶段！", "warning");
        return;
    }else if(sort == ''){
        swal("警告","请输入课程排序！", "warning");
        return;
    }
    $.ajax({
        type: "POST",
        url: "update",
        data: {id:id,title:title,introduce:introduce,courseUrl:courseUrl,courseSections:courseSections,courseTime:courseTime,courseTypeId:courseTypeId,courseStageId:courseStageId,status:0,sort:sort},
        dataType: "json",
        success: function(data){
            if (data['code']== 200){
                swal("保存草稿成功！", "", "success")
                    .then(() => {
                        var location = window.location.href;
                        let strings = location.split("iStudy");
                        window.location.href = strings[0]+"iStudy/edit?id="+data['data'];
                    });
            }else{
                swal("出错啦", "服务器发生了一个错误", "error");
            }
        }
    });
}

/**
 * 发表评论
 */
function addComment(id) {
    let name = $('#name').val().trim();
    let email = $('#email').val().trim();
    let content = $('#content').val().trim();
    if (name == ''){
        swal("警告","昵称不能为空！", "warning");
        return;
    }
    if (email == ''){
        swal("警告","邮箱不能为空！", "warning");
        return;
    }
    if (content == ''){
        swal("警告","留言内容不能为空！", "warning");
        return;
    }
    var emailReg = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
    if(!emailReg.test(email)){
        swal("警告", "请输入正确的邮箱格式", "warning");
        return;
    }
    $.ajax({
        type: "POST",
        url: "addComment",
        data: {name:name,email:email,content:content,articleId:id},
        dataType: "json",
        success: function(data){
            if (data['code']== 200){
                swal("成功", "发表评论成功，请等待作者回复！", "success");
                setInterval(reload, 2000);
            }else{
                swal("出错啦", "服务器发生了一个错误", "error");
            }
        }
    });

}

/**
 * 刷新页面
 */
function reload() {
    window.location.reload();
}

/**
 * 监听选择课程类别和课程阶段变化
 */
function changeCourseType(){
    var courseTypeId =  $('#type option:selected').val();
    var courseStageJson = [];
    $.ajax({
        type: "POST",
        url: "getCourseStage",
        data: {courseTypeId:courseTypeId},
        dataType: "json",
        success: function(data){
            let courseStageSelectObj = document.getElementById("stage");
            courseStageSelectObj.options.length = 0;
            courseStageSelectObj.options.add(new Option("请选择课程阶段",0));
            if (data['code']== 200){
                courseStageJson = data.data;
                for(let i=0;i<courseStageJson.length;i++){
                    courseStageSelectObj.add(new Option(courseStageJson[i].title,courseStageJson[i].id));
                }
            }
        }
    });
}
